package com.stats.ultimate;

import android.content.*;
import android.database.sqlite.*;
import android.widget.Toast;
import java.util.*;


public class DatabaseHelper extends SQLiteOpenHelper {
	// Make strings to hold the path to the database and the database name
	private static final String DB_PATH = "/data/data/com.stats.ultimate/databases/";
	private static final String DB_NAME = "ultimate.db";
	
	// Create variables to hold the context
	Context myContext;
	
	// Make constructor
	public DatabaseHelper(Context context){
		// Call super function and set current context
		super(context, DB_NAME, null, 1);
		myContext = context;
	}
	
	// Override onCreate function
	@Override
	public void onCreate(SQLiteDatabase db){
		// Add teams table
		db.execSQL("CREATE TABLE IF NOT EXISTS teams (_id INTEGER PRIMARY KEY, name TEXT, num_players NUMERIC, table_name TEXT);");
		
		// Add db_info table
		db.execSQL("CREATE TABLE IF NOT EXISTS db_info (_id INTEGER PRIMARY KEY, num_teams NUMERIC, num_total NUMERIC);");
		
		// Add only row to db_info table
		SQLiteStatement statement = db.compileStatement("INSERT INTO db_info VALUES(1,0,0);");
		statement.executeInsert();
	}
	
	// Override onUpgrade function
	@Override
	public void onUpgrade(SQLiteDatabase database, int old_version, int new_version){
	}
	
	// Make a function to read number of teams from the database
	public int getNumberTeams(SQLiteDatabase db){
		// Initialize return value to -1
		Long result = new Long(-1);
		
		// Check if db is null
		if(db == null){
			Toast.makeText(myContext, "Error: Unable to read number of teams from database", Toast.LENGTH_SHORT).show();
			return result.intValue();
		}
		
		// Otherwise prepare a statement to read num_users from db_info table
		SQLiteStatement statement = db.compileStatement("SELECT num_teams FROM db_info WHERE _id = 1;");
		
		// Execute statement and save it to a long variable
		result = statement.simpleQueryForLong();
		
		// Return int value of results
		return result.intValue();
	}
	
	// Make a function to read total teams from the database
	public int getNumberTotal(SQLiteDatabase db){
		// Initialize return value to -1
		Long result = new Long(-1);
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to read number of total teams from database", Toast.LENGTH_SHORT).show();
			return result.intValue();
		}
		
		// Otherwise prepare a statement to read num_total from db_info table
		SQLiteStatement statement = db.compileStatement("SELECT num_total FROM db_info WHERE _id = 1;");
		
		// Execute statement and save result
		result = statement.simpleQueryForLong();
		
		// Return result
		return result.intValue();
	}
	
	// Make a function to read a teams name from the database
	public String getTeamName(SQLiteDatabase db, int id){
		// Check if db is null
		if(db == null){
			// If so give an error message and return null
			Toast.makeText(myContext, "Error: Unable to retrieve teams name from database", Toast.LENGTH_SHORT).show();
			return null;
		}
		
		// Otherwise prepare a statement to read name from database
		SQLiteStatement statement = db.compileStatement("SELECT name FROM teams WHERE _id = ?;");
		
		// Bind id to statement
		statement.bindLong(1, id);
		
		// Execute statement and return result
		return statement.simpleQueryForString();
	}
	
	// Make a function to read number of players on a team from the database
	public int getNumberPlayers(SQLiteDatabase db, int id){
		// Initialize return value to -1
		Long result = new Long(-1);
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to read number of players from database", Toast.LENGTH_SHORT).show();
			return result.intValue();
		}
		
		// Otherwise compile and save sqlite statement
		SQLiteStatement statement = db.compileStatement("SELECT num_players FROM teams WHERE _id = ?;");
		
		// Bind name to sqlite statement
		statement.bindLong(1, id);
		
		// Execute statement and save result
		result = statement.simpleQueryForLong();
		
		// Return result
		return result.intValue();
	}
	
	// Make a function to get a team's players table name from the database
	public String getTableName(SQLiteDatabase db, int id){
		// Initialize return value to null
		String results = null;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return null
			Toast.makeText(myContext, "Error: Unable to access database to read team's table name", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise prepare a statement to get table name from database
		SQLiteStatement statement = db.compileStatement("SELECT table_name FROM teams WHERE _id = ?;");
		
		// Bind id to statement
		statement.bindLong(1, id);
		
		// Execute statement and save return value
		results = statement.simpleQueryForString();
		
		// Return results
		return results;
	}
	
	// Make function to get a player's name from the database
	public String getPlayerName(SQLiteDatabase db, int team_id, int player_id){
		// Initialize return value to null
		String results = null;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return null
			Toast.makeText(myContext, "Error: Unable to retrieve player's name from database", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get team's table name from database
		String name = getTableName(db, team_id);
		
		// Check if name is null
		if(name == null){
			// If so return null
			return results;
		}
		
		// Otherwise prepare statement to retrieve this players name
		SQLiteStatement statement = db.compileStatement("SELECT name FROM "+name+" WHERE _id = ?;");
		
		// Bind player_id to statement
		statement.bindLong(1, player_id);
		
		// Execute statement and save result
		results = statement.simpleQueryForString();
		
		// Return results
		return results;
	}
	
	// Make function to read a team's stats from the database
	public int getController(SQLiteDatabase db, int id, String name){
		// Create variable to hold return value
		int results = -1;
		
		// Compile and save sqlite statement
		SQLiteStatement statement = db.compileStatement("SELECT controls FROM users WHERE _id = ?;");
		
		// Bind id value to sqlite statement
		statement.bindLong(1, id);
		
		// Execute statement and save results to temporary variable
		Long temp = statement.simpleQueryForLong();
		
		// Save results of query as an int into results
		results = temp.intValue();
		
		// Return results
		return results;
	}
	
	// Make function to read a player's stats from the database
	public int[] getPlayerStats(SQLiteDatabase db, int team_id, int player_id){
		// Initialize return value to array with all -1's
		int results[] = new int[4];
		results[0] = -1;
		results[1] = -1;
		results[2] = -1;
		results[3] = -1;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database to read players stats", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get the table name for this team
		String table = getTableName(db, team_id);
		
		// Check if table is null
		if(table == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Could not access players table for this team", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise make variable to hold SQLite Statements
		SQLiteStatement statement;
		
		// Compile and save a statement to get this players goals
		statement = db.compileStatement("SELECT goals FROM "+table+" WHERE _id = ?;");
		
		// Bind player_id value to statement
		statement.bindLong(1, player_id);
		
		// Execute statement and save value to results
		Long temp = statement.simpleQueryForLong();
		results[0] = temp.intValue();
		
		// Compile and save statement to get this players assists
		statement = db.compileStatement("SELECT assists FROM "+table+" WHERE _id = ?;");
		
		// Bind player_id value to statement
		statement.bindLong(1, player_id);
		
		// Execute statement and save value to results
		temp = statement.simpleQueryForLong();
		results[1] = temp.intValue();
		
		// Compile and save statement to get this players defenses
		statement = db.compileStatement("SELECT defenses FROM "+table+" WHERE _id = ?;");
		
		// Bind player_id value to statement
		statement.bindLong(1, player_id);
		
		// Execute statement and save value to results
		temp = statement.simpleQueryForLong();
		results[2] = temp.intValue();
		
		// Compile and save statement to get this players turnovers
		statement = db.compileStatement("SELECT turnovers FROM "+table+" WHERE _id = ?;");
		
		// Bind player_id value to statement
		statement.bindLong(1, player_id);
		
		// Execute statement and save value to results
		temp = statement.simpleQueryForLong();
		results[3] = temp.intValue();
		
		// Return results
		return results;
	}
	
	// Make a function to change the number of teams in the database
	public int updateNumTeams(SQLiteDatabase db, int num){
		// Check if db is null
		if(db == null){
			// If so give error message and return -1
			Toast.makeText(myContext, "Error: Unable to update number of teams in database", Toast.LENGTH_SHORT).show();
			return -1;
		}
		
		// Otherwise prepare a statement to set the new number of teams in the database
		SQLiteStatement statement = db.compileStatement("UPDATE db_info SET num_teams = ? WHERE _id = 1;");
		
		// Bind num to statement
		statement.bindLong(1, num);
		
		// Execute statement
		statement.execute();
		
		// Return 0 for success
		return 0;
	}
	
	// Make a function to change the number of total teams in the database
	public int updateNumTotal(SQLiteDatabase db, int num){
		// Initialize return value to -1
		int result = -1;
		
		// Check if db is null
		if(db == null){
			// If so give error message and return -1
			Toast.makeText(myContext, "Error: Unable to update number of total teams in database", Toast.LENGTH_SHORT).show();
			return result;
		}
		
		// Otherwise prepare a statement to set the new number of teams in the database
		SQLiteStatement statement = db.compileStatement("UPDATE db_info SET num_total = ? WHERE _id = 1;");
		
		// Bind num to statement
		statement.bindLong(1, num);
		
		// Execute statement
		statement.execute();
		
		// Return 0 for success
		result = 0;
		return result;
	}
	
	// Make a function to update the name of a team and its players in the database
	public int updateTeam(SQLiteDatabase db, int id, String new_name, ArrayList<String> players, ArrayList<String>new_players){
		// Initialize return value to -1
		int results = -1;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database to update team info", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get this team's table name from database
		String table_name = getTableName(db, id);
		
		// Check if table_name is null
		if(table_name == null){
			// If so return -1
			return results;
		}
		
		// Otherwise prepare a statement to update the name of this team in the teams table
		SQLiteStatement statement = db.compileStatement("UPDATE teams SET name = ? WHERE _id = ?;");
		
		// Bind new team name to first spot
		statement.bindString(1, new_name);
		
		// Bind id to second spot
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// for loop to increment through players array
		for(int i = 0; i < players.size(); i++){
			// Prepare a statement to update the name of this player in the database
			statement = db.compileStatement("UPDATE "+table_name+" SET name = ? WHERE _id = ?;");
			
			// Bind players new name to first spot
			statement.bindString(1, players.get(i));
			
			// Bind players id to second spot
			statement.bindLong(2, i+1);
			
			// Execute statement
			statement.execute();
		}
		
		// for loop to increment through new_players array
		for(int i = 0; i < new_players.size(); i++){
			// Add this player to the team and save result
			results = addPlayer(db, new_players.get(i), id);
			
			// Check if results is negative
			if(results < 0){
				// If so give an error message and return -1
				Toast.makeText(myContext, "Error: Unable to add new players to team", Toast.LENGTH_SHORT).show();
				return results;
			}
		}
		
		// Return 0 for success
		results = 0;
		return results;
	}
	
	// Make function to change a player's stats in the database
	public int updateStats(SQLiteDatabase db, int team_id, int player_id, int[] stats){
		// Initialize return value to -1
		int results = -1;
		
		// Check if db is null
		if(db == null){
			// If so then give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database to change a player's stats", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get this teams players table name
		String table = getTableName(db, team_id);
		
		// Check if table is null
		if(table == null){
			// If so return -1
			return results;
		}
		
		// Otherwise compile a statement to change this players goals
		SQLiteStatement statement = db.compileStatement("UPDATE "+table+" SET goals = ? WHERE _id = ?;");
		
		// Bind new value for goals to first spot
		statement.bindLong(1, stats[0]);
		
		// Bind player_id to second spot
		statement.bindLong(2, player_id);
		
		// Execute statement
		statement.execute();
		
		// Compile a statement to change players assists
		statement = db.compileStatement("UPDATE "+table+" SET assists = ? WHERE _id = ?;");
		
		// Bind new value for assists to first spot
		statement.bindLong(1, stats[1]);
		
		// Bind player_id to second spot
		statement.bindLong(2, player_id);
		
		// Execute statement
		statement.execute();
		
		// Compile a statement to change players d
		statement = db.compileStatement("UPDATE "+table+" SET defenses = ? WHERE _id = ?;");
		
		// Bind new value for d to first spot
		statement.bindLong(1, stats[2]);
		
		// Bind player_id to second spot
		statement.bindLong(2, player_id);
		
		// Execute statement
		statement.execute();
		
		// Compile a statement to change players to
		statement = db.compileStatement("UPDATE "+table+" SET turnovers = ? WHERE _id = ?;");
		
		// Bind new value for to to first spot
		statement.bindLong(1, stats[3]);
		
		// Bind player_id to second spot
		statement.bindLong(2, player_id);
		
		// Execute statement
		statement.execute();
		
		// Return 0 for success
		results = 0;
		return results;
	}
	
	// Make function to change a team's stats in the database
	public void setConfiguration(SQLiteDatabase db, int id, ArrayList<String> commands){
		// Make variable to hold SQLite statements
		SQLiteStatement statement;
		
		// Compile and save statement to update accelerate in the database
		statement = db.compileStatement("UPDATE users SET accelerate = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(0));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update brake in the database
		statement = db.compileStatement("UPDATE users SET brake = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(1));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update horn in the database
		statement = db.compileStatement("UPDATE users SET horn = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(2));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update left in the database
		statement = db.compileStatement("UPDATE users SET left = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(3));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update right in the database
		statement = db.compileStatement("UPDATE users SET right = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(4));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update warning in the database
		statement = db.compileStatement("UPDATE users SET warning = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(5));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update left_signal in the database
		statement = db.compileStatement("UPDATE users SET left_signal = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(6));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update right_signal in the database
		statement = db.compileStatement("UPDATE users SET right_signal = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(7));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update lights in the database
		statement = db.compileStatement("UPDATE users SET lights = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(8));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update gear_up in the database
		statement = db.compileStatement("UPDATE users SET gear_up = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(9));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
		
		// Compile and save statement to update accelerate in the database
		statement = db.compileStatement("UPDATE users SET gear_down = ? WHERE _id = ?;");
		
		// Bind values for id and new command to statement
		statement.bindString(1, commands.get(10));
		statement.bindLong(2, id);
		
		// Execute statement
		statement.execute();
	}
	
	// Make a function to add a team to the database
	public int addTeam(SQLiteDatabase db, String name, ArrayList<String> players){
		// Check if db is null
		if(db == null){
			// If so return give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database", Toast.LENGTH_SHORT).show();
			return -1;
		}
		
		// Otherwise get current number of teams in db
		int num_teams = getNumberTeams(db);
		
		// Check if num_teams is negative
		if(num_teams < 0){
			// If so return -1
			return -1;
		}
		
		// Otherwise declare statement variable to hold sqlite statements
		SQLiteStatement statement;
		
		// Increment through database to check names of teams already in the database
		for(int i = 1; i <= num_teams; i++){
			// Compile a statement to read name of team with this id number from teams table
			statement = db.compileStatement("SELECT name FROM teams WHERE _id = ?;");
			
			// Bind current id number to first spot
			statement.bindLong(1, i);
			
			// Execute querry and save result to temp variable
			String temp = statement.simpleQueryForString();
			
			// Check if current team's name is same as name to be inserted
			if(temp.equalsIgnoreCase(name)){
				// If so give an error message and return -1
				Toast.makeText(myContext, "Team name already exists, choose another name", Toast.LENGTH_SHORT).show();
				return -1;
			}
		}
		
		// If this team's name isn't a duplicate then get the number of players to be added to this team
		int num_players = players.size();
		
		// Get number of teams ever entered into database from database
		int total = getNumberTotal(db);
		
		// Check if this returned negative
		if(total < 0){
			// If so return -1
			return -1;
		}
		
		// Otherwise prepare the string for the table name for this team
		String table = String.format("players%d", ++total);
		
		// Otherwise compile a statement to insert this team into teams table
		statement = db.compileStatement("INSERT INTO teams VALUES(?, ?, 0, ?);");
		
		// Bind the id for this team to the first spot
		statement.bindLong(1, ++num_teams);
		
		// Bind the name for this team to the second spot
		statement.bindString(2, name);
		
		// Bind the table name for this team to the third spot
		statement.bindString(3, table);
		
		// Execute statement to insert this team into teams table
		statement.execute();
		
		// Update number of teams in the database
		int update = updateNumTeams(db, num_teams);
		
		// Check if update was unsuccessful
		if(update < 0){
			// If so prepare statement to remove previously added team from database
			db.delete("teams", "_id = "+num_teams, null);
			
			return -1;
		}
		
		// Update number of total teams in the database
		update = updateNumTotal(db, total);
		
		// Check if update was unsuccessful
		if(update < 0){
			// If so return -1
			return -1;
		}
		
		// Otherwise create table for this teams players
		db.execSQL("CREATE TABLE IF NOT EXISTS "+table+" (_id INTEGER PRIMARY KEY, name TEXT, goals NUMERIC, assists NUMERIC, defenses NUMERIC, turnovers NUMERIC);");
		
		// for loop to increment through all the players in players array
		for(int i = 0; i < num_players; i++){
			// Insert this player into the database and save value
			int temp = addPlayer(db, players.get(i), num_teams);
			
			// Check if the insert failed
			if(temp < 0){
				// If so return -1
				return -1;
			}
		}
		
		// Return 0 for success
		return 0;
	}
	
	// Make a function to add a player to an existing team
	public int addPlayer(SQLiteDatabase db, String player, int team_id){
		// Initialize return value to -1
		Long result = new Long(-1);
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database", Toast.LENGTH_SHORT).show();
			return result.intValue();
		}
		
		// Otherwise get this teams table name
		String table = getTableName(db, team_id);
		
		// Check if team is null
		if(table == null){
			// If so return -1
			return result.intValue();
		}
		
		// Otherwise get number of players from current team
		int num_players = getNumberPlayers(db, team_id);
		
		// Prepare statement to increment number of players on this team
		SQLiteStatement statement = db.compileStatement("UPDATE teams SET num_players = ? WHERE _id = ?;");
		
		// Bind incremented number of players to statement
		statement.bindLong(1, ++num_players);
		
		// Bind team_id to statement
		statement.bindLong(2, team_id);
		
		// Execute statement
		statement.execute();
		
		// Prepare statement to add player to team
		statement = db.compileStatement("INSERT INTO "+table+" VALUES(?, ?, 0, 0, 0, 0);");
		
		// Bind players id to first spot
		statement.bindLong(1, num_players);
		
		// Bind players name to second spot
		statement.bindString(2, player);
		
		// Execute statement and save return value
		result = statement.executeInsert();
		
		// Return result of query
		return result.intValue();
	}
	
	// Make a function to delete a team from the database
	public int deleteTeam(SQLiteDatabase db, int id){
		// Initialize return value to -1
		int results = -1;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database to delete team", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get this team's table name from the database
		String table = getTableName(db, id);
		
		// Check if table is null
		if(table == null){
			// If so return -1
			return -1;
		}
		
		// Otherwise delete this team's players table in database
		db.execSQL("DROP TABLE IF EXISTS "+table+";");
		
		// Format string to hold id
		String id_string = String.format("%d", id);
		
		// Delete this team from teams table and save result
		results = db.delete("teams", "_id = ?", new String[]{id_string});
		
		// Check if results is not 1
		if(results != 1){
			// If so then give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to delete team from database", Toast.LENGTH_SHORT).show();
			results = -1;
			return results;
		}
		
		// Otherwise get number of teams from database
		int num_teams = getNumberTeams(db);
		
		// Try to update the number of teams in the database
		if(updateNumTeams(db, num_teams - 1) < 0){
			// If this is unsuccessful then give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to update number of teams in the database", Toast.LENGTH_SHORT).show();
			results = -1;
			return results;
		}
		
		// Otherwise for loop to increment through all teams after this one in the database
		for(int i = id + 1; i <= num_teams; i++){
			// Get name of current team
			String temp_name = getTeamName(db, i);
			
			// Get number of players on current team
			int num_players = getNumberPlayers(db, i);
			
			// Format id_string
			id_string = String.format("%d", i);
			
			// Delete this team from the database and save results
			results = db.delete("teams", "_id = ?", new String[]{id_string});
			
			// Check if results is not 1
			if(results != 1){
				// If so then give an error message and return -1
				Toast.makeText(myContext, "Error: Unable to reorganize database after deleting team", Toast.LENGTH_SHORT).show();
				results = -1;
				return results;
			}
			
			// Prepare statement to re-insert this team into the database
			SQLiteStatement statement = db.compileStatement("INSERT into teams Values(?, ?, ?);");
			
			// Bind the new id for this team to the first spot
			statement.bindLong(1, i-1);
			
			// Bind the name for this team to the second spot
			statement.bindString(2, temp_name);
			
			// Bind the number of players for this team to the second spot
			statement.bindLong(3, num_players);
			
			// Execute insert and save return value
			Long temp = statement.executeInsert();
			
			// Check if insert returned negative
			if(temp.intValue() < 0){
				// If so give an error message and return -1
				Toast.makeText(myContext, "Error: Unable to reorganize database after deleting team", Toast.LENGTH_SHORT).show();
				results = -1;
				return results;
			}
		}
		
		// Return 0 for success
		results = 0;
		return results;
	}
	
	// Make a function to delete a player from the database
	public int deletePlayer(SQLiteDatabase db, int team_id, int player_id){
		// Initialize return value to -1
		int results = -1;
		
		// Check if db is null
		if(db == null){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to access database to delete player", Toast.LENGTH_SHORT).show();
			return results;
		}
		
		// Otherwise get team's table name from database
		String name = getTableName(db, team_id);
		
		// Check if name is null
		if(name == null){
			// If so return -1
			return results;
		}
		
		// Format String to hold where clause
		String where = String.format("_id = %d", player_id);
		
		// Otherwise delete this row from the teams table and save results
		results = db.delete(name, where, null);
		
		// Check if results is not 1
		if(results != 1){
			// If so give an error message and return -1
			Toast.makeText(myContext, "Error: Unable to delete player from database", Toast.LENGTH_SHORT).show();
			results = -1;
			return results;
		}
		
		// Otherwise read number of players for this team from the database
		int num_players = getNumberPlayers(db, team_id);
		
		// Prepare a statement to update the number of players for this team in the database
		SQLiteStatement statement = db.compileStatement("UPDATE teams SET num_players = ? WHERE _id = ?;");
		
		// Bind the new value for number of players to the first spot
		statement.bindLong(1, num_players - 1);
		
		// Bind the id of this team to the second spot
		statement.bindLong(2, team_id);
		
		// Execute the statement
		statement.execute();
		
		// for loop to increment through all players after player that was deleted
		for(int i = player_id + 1; i <= num_players; i++){
			// Get this players name
			String temp_name = getPlayerName(db, team_id, i);
			
			// Get this players stats
			int stats[] = new int[4];
			stats = getPlayerStats(db, team_id, i);
			
			// Format String to hold where clause
			where = String.format("_id = %d", i);
			
			// Delete this row from the teams table and save results
			results = db.delete(name, where, null);
			
			// Check if results is not 1
			if(results != 1){
				// If so give an error message and return -1
				Toast.makeText(myContext, "Error: Unable to re-organize database after deletion of player", Toast.LENGTH_SHORT).show();
				results = -1;
				return results;
			}
			
			// Prepare a statement to re-insert this player with a new id
			statement = db.compileStatement("INSERT into "+name+" VALUES(?, ?, ?, ?, ?, ?);");
			
			// Bind the new id value for this player to the first spot
			statement.bindLong(1, i-1);
			
			// Bind this players name to the second spot
			statement.bindString(2, temp_name);
			
			// Bind this players goals to third spot
			statement.bindLong(3, stats[0]);
			
			// Bind this players assists to fourth spot
			statement.bindLong(4, stats[1]);
			
			// Bind this players defenses to fifth spot
			statement.bindLong(5, stats[2]);
			
			// Bind this players turnovers to sixth spot
			statement.bindLong(6, stats[3]);
			
			// Execute the statement and save result
			Long temp = statement.executeInsert();
			
			// Check if insert returned negative
			if(temp.intValue() < 0){
				// If so give an error message and return -1
				Toast.makeText(myContext, "Error: Unable to re-organize database after deletion of player", Toast.LENGTH_SHORT).show();
				results = -1;
				return results;
			}
		}
		
		// Return 0 for success
		results = 0;
		return results;
	}
}


